import { NextFunction, Request, Response } from "express";
import { TagService } from "../services";
import { Like } from "typeorm";
import { paginate } from "../utils";

export class TagController {
  public static async findMany(
    req: Request,
    res: Response,
    next: NextFunction,
  ) {
    try {
      const tags = await TagService.find({
        ...paginate(req),
        where: [{ tag: Like(`%${req.query.q || ""}%`) }],
      });
      res.json({
        data: tags[0],
        total: tags[1],
      });
    } catch (err) {
      next(err);
    }
  }
}
